package com.styletransfer.mapper;

import com.styletransfer.model.Album;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 专辑数据访问接口
 */
@Mapper
public interface AlbumMapper {

    /**
     * 插入专辑
     *
     * @param album 专辑对象
     * @return 影响的行数
     */
    @Insert({
        "INSERT INTO album (user_id, title, description, cover_image, image_count, like_count, comment_count, view_count, create_time, update_time, status, is_public)",
        "VALUES (#{userId}, #{title}, #{description}, #{coverImage}, #{imageCount}, #{likeCount}, #{commentCount}, #{viewCount}, #{createTime}, #{updateTime}, #{status}, #{isPublic})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Album album);

    /**
     * 插入专辑图片关联
     *
     * @param albumId 专辑ID
     * @param imageUrl 图片URL
     * @param sortOrder 排序顺序
     * @return 影响的行数
     */
    @Insert({
        "INSERT INTO album_image (album_id, image_url, sort_order, create_time)",
        "VALUES (#{albumId}, #{imageUrl}, #{sortOrder}, NOW())"
    })
    int insertAlbumImage(@Param("albumId") Long albumId, @Param("imageUrl") String imageUrl, @Param("sortOrder") Integer sortOrder);

    /**
     * 根据ID查询专辑
     *
     * @param id 专辑ID
     * @return 专辑对象
     */
    @Select({
        "SELECT * FROM album WHERE id = #{id} AND status = 1"
    })
    Album findById(Long id);

    /**
     * 查询专辑的图片列表
     *
     * @param albumId 专辑ID
     * @return 图片URL列表
     */
    @Select({
        "SELECT image_url FROM album_image WHERE album_id = #{albumId} ORDER BY sort_order ASC"
    })
    List<String> findAlbumImages(Long albumId);

    /**
     * 根据用户ID查询专辑列表
     *
     * @param userId 用户ID
     * @return 专辑列表
     */
    @Select({
        "SELECT * FROM album WHERE user_id = #{userId} AND status = 1 ORDER BY create_time DESC"
    })
    List<Album> findByUser(Long userId);

    /**
     * 分页查询用户的专辑
     *
     * @param userId 用户ID
     * @param offset 偏移量
     * @param limit 限制数
     * @return 专辑列表
     */
    @Select({
        "SELECT * FROM album WHERE user_id = #{userId} AND status = 1",
        "ORDER BY create_time DESC LIMIT #{offset}, #{limit}"
    })
    List<Album> findByUserPage(@Param("userId") Long userId, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 查询用户的专辑总数
     *
     * @param userId 用户ID
     * @return 专辑总数
     */
    @Select({
        "SELECT COUNT(*) FROM album WHERE user_id = #{userId} AND status = 1"
    })
    Long countByUser(Long userId);

    /**
     * 查询公开的专辑
     *
     * @return 专辑列表
     */
    @Select({
        "SELECT * FROM album WHERE is_public = 1 AND status = 1 ORDER BY create_time DESC"
    })
    List<Album> findPublic();

    /**
     * 分页查询公开的专辑
     *
     * @param offset 偏移量
     * @param limit 限制数
     * @return 专辑列表
     */
    @Select({
        "SELECT * FROM album WHERE is_public = 1 AND status = 1",
        "ORDER BY create_time DESC LIMIT #{offset}, #{limit}"
    })
    List<Album> findPublicPage(@Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 查询公开专辑的总数
     *
     * @return 专辑总数
     */
    @Select({
        "SELECT COUNT(*) FROM album WHERE is_public = 1 AND status = 1"
    })
    Long countPublic();

    /**
     * 更新专辑
     *
     * @param album 专辑对象
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET",
        "title = #{title},",
        "description = #{description},",
        "cover_image = #{coverImage},",
        "is_public = #{isPublic},",
        "update_time = #{updateTime}",
        "WHERE id = #{id}"
    })
    int update(Album album);

    /**
     * 更新专辑状态
     *
     * @param id 专辑ID
     * @param status 状态
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET status = #{status}, update_time = NOW() WHERE id = #{id}"
    })
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);

    /**
     * 增加浏览次数
     *
     * @param id 专辑ID
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET view_count = view_count + 1 WHERE id = #{id}"
    })
    int incrementViewCount(Long id);

    /**
     * 增加点赞数
     *
     * @param id 专辑ID
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET like_count = like_count + 1 WHERE id = #{id}"
    })
    int incrementLikeCount(Long id);

    /**
     * 减少点赞数
     *
     * @param id 专辑ID
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET like_count = GREATEST(like_count - 1, 0) WHERE id = #{id}"
    })
    int decrementLikeCount(Long id);

    /**
     * 增加评论数
     *
     * @param id 专辑ID
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET comment_count = comment_count + 1 WHERE id = #{id}"
    })
    int incrementCommentCount(Long id);

    /**
     * 减少评论数
     *
     * @param id 专辑ID
     * @return 影响的行数
     */
    @Update({
        "UPDATE album SET comment_count = GREATEST(comment_count - 1, 0) WHERE id = #{id}"
    })
    int decrementCommentCount(Long id);

    /**
     * 查询用户是否点赞了专辑
     *
     * @param userId 用户ID
     * @param albumId 专辑ID
     * @return 点赞记录数
     */
    @Select({
        "SELECT COUNT(*) FROM user_like WHERE userId = #{userId} AND targetId = #{albumId} AND type = '4'"
    })
    int isLiked(@Param("userId") Long userId, @Param("albumId") Long albumId);
    
    /**
     * 查询用户的所有专辑ID
     *
     * @param userId 用户ID
     * @return 专辑ID列表
     */
    @Select({
        "SELECT id FROM album WHERE user_id = #{userId} AND status = 1"
    })
    List<Long> findAlbumIdsByUserId(Long userId);
}
